﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace NNDC
{
    public partial class DC : Form
    {
        public DC()
        {
            InitializeComponent();
            this.skinEngine1.SkinFile = "OneOrange.ssk";
        }
        public string RName;
        private void DC_Load(object sender, EventArgs e)
        {
            this.Text = RName + "点/加菜";
            TreeNode newnode1 = tvFood.Nodes.Add("锅底");
            TreeNode newnode2 = tvFood.Nodes.Add("配菜");
            TreeNode newnode3 = tvFood.Nodes.Add("烟酒");
            TreeNode newnode4 = tvFood.Nodes.Add("主食");
            SqlConnection conn = data.DBConn.Cycon();
            conn.Open();
            SqlCommand cmd = new SqlCommand("select * from tb_food where foodty='1'",conn);
            SqlDataReader sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                newnode1.Nodes.Add(sdr[3].ToString().Trim());
            }
            sdr.Close();
            cmd = new SqlCommand("select * from tb_food where foodty='2'", conn);
            sdr = cmd.ExecuteReader();
            while(sdr.Read()){
                newnode2.Nodes.Add(sdr[3].ToString().Trim());
            }
            sdr.Close();
            cmd = new SqlCommand("select * from tb_food where foodty='3'", conn);
            sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                newnode3.Nodes.Add(sdr[3].ToString().Trim());
            }
            sdr.Close();
            cmd = new SqlCommand("select * from tb_food where foodty='4'", conn);
            sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                newnode4.Nodes.Add(sdr[3].ToString().Trim());
            }
            sdr.Close();
            cmd = new SqlCommand("select * from tb_waiter",conn);
            sdr = cmd.ExecuteReader();
            while(sdr.Read()){
                cbWaiter.Items.Add(sdr["WaiterName"].ToString().Trim());
            }
            cbWaiter.SelectedIndex = 0;
            sdr.Close();
            cmd = new SqlCommand("select RoomZT from tb_Room where RoomName='"+RName+"'",conn);
            string zt = Convert.ToString(cmd.ExecuteScalar());
            if(zt.Trim()=="待用"){
                groupBox1.Enabled = false;
                groupBox2.Enabled = false;
                groupBox3.Enabled = false;
                groupBox4.Enabled = false;
            }
            conn.Close();
            GetData();
            tvFood.ExpandAll();
        }

        private void GetData()
        {
            SqlConnection conn = data.DBConn.Cycon();
            SqlDataAdapter sda = new SqlDataAdapter("select foodname,foodsum,foodallprice,waitername,beizhu,zhuotai,datatime from tb_GuestFood where zhuotai='"+RName+"'order by ID desc",conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            dgvFoods.DataSource = ds.Tables[0];
        }

        private void tvFood_DoubleClick(object sender, EventArgs e)
        {
            string foodname = tvFood.SelectedNode.Text;
            if (foodname == "锅底" || foodname == "配菜" || foodname == "烟酒" || foodname == "主食")
            {

            }
            else
            {

                SqlConnection conn = data.DBConn.Cycon();
                conn.Open();
                SqlCommand cmd = new SqlCommand("select * from tb_food where foodname='"+foodname+"'",conn);
                SqlDataReader sdr = cmd.ExecuteReader();
                sdr.Read();
                num.Text = sdr["foodnum"].ToString().Trim();
                name.Text = foodname;
                price.Text = sdr["foodprice"].ToString().Trim();
                conn.Close();
                if (pNum.Text == "")
                {
                    MessageBox.Show("请输入数量");
                    return;
                }
                else
                {
                    allprice.Text = Convert.ToString(
                        Convert.ToInt32(price.Text)*Convert.ToInt32(pNum.Text)
                        );
                }
            }
        }

        private void pNum_TextChanged(object sender, EventArgs e)
        {
            if (pNum.Text == "")
            {
                MessageBox.Show("请输入数量");
                return;
            }
            else
            {
                if (Convert.ToInt32(pNum.Text) < 1)
                {
                    MessageBox.Show("输入数量不能小于1");
                    return;
                }
                else
                {
                    allprice.Text = Convert.ToString(
                        Convert.ToInt32(price.Text)*Convert.ToInt32(pNum.Text)
                        );
                }
            }
        }

        private void pNum_KeyPress(object sender, KeyPressEventArgs e)
        {
            if((e.KeyChar!=8&&!char.IsDigit(e.KeyChar))&&e.KeyChar!=13){
                MessageBox.Show("请输入数字");
                e.Handled = true;
            }
        }

        private void delete_Click(object sender, EventArgs e)
        {
            if(dgvFoods.SelectedRows.Count>0){
                string names = dgvFoods.SelectedCells[0].Value.ToString();
                SqlConnection conn = data.DBConn.Cycon();
                conn.Open();
                SqlCommand cmd = new SqlCommand("delete from tb_GuestFood where foodname"+names+"'and zhuotai='"+RName+"'",conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                GetData();
            }
        }

        private void save_Click(object sender, EventArgs e)
        {
            if (name.Text == "" || num.Text == "" || price.Text == "")
            {
                MessageBox.Show("请选择菜系");
                return;
            }
            else
            {
                if (Convert.ToInt32(pNum.Text) <= 0)
                {
                    MessageBox.Show("请输入消费数量");
                    return;
                }
                else
                {
                    SqlConnection conn = data.DBConn.Cycon();
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("insert into tb_GuestFood(foodnum,foodname,foodsum,foodallprice,waitername,beizhu,zhuotai,datatime) values('" 
                        + num.Text.Trim() + "','" + name.Text.Trim() + "','"
                        + pNum.Text.Trim() + "','" + Convert.ToDecimal(allprice.Text.Trim()) 
                        +  "','" + cbWaiter.SelectedItem.ToString() + "','" + bz.Text.Trim()
                        +  "','" + RName + "','" + DateTime.Now.ToString() + "')", conn);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                    GetData();
                }
            }
        }

        private void exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
